<?php
    class search_view_controller extends controllerBase
    {
        public function process()
        {
            $oDatabase = new database();
            // Get all province location
            $sQuery = "SELECT * FROM location ORDER BY name ASC";
            $aLocations = $oDatabase->getListObject($sQuery,true,true);
            if(is_array($aLocations))
            {
                $this->aParams['aLocations'] = $aLocations;
            }

            // Get all manufacturer
            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT *  
            WHERE {
            ?uri rdf:type bus:manufacturer.
            ?uri bus:name ?name.
            }";

            $aResults = $oDatabase->loadQuery($sQuery);
            if(!empty($aResults['result']['rows']))
            {
                $this->aParams['aManufacturers'] = $aResults['result']['rows'];
            }

            // Neu keyword ton tai thi tim kiem theo keyword
            if($this->iId != '')
            {
                $sQuery = "
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX foaf: <http://xmlns.com/foaf/0.1/>
                PREFIX bus: <".URL::getLink('id/bussiness#').">

                SELECT DISTINCT ?uri ?name ?price ?description ?ownUser ?image        
                WHERE 
                {
                ?uri rdf:type bus:product.
                ?uri bus:name ?name.
                ?uri bus:price ?price.
                ?uri bus:image ?image.
                ?uri bus:hasManufacturer ?manufacturer.
                ?uri bus:description ?description.
                OPTIONAL{?uri bus:location ?location.}
                ?uri bus:ownUser ?userUri.
                ?userUri foaf:familyName ?ownUser.
                ?uri bus:childCategoryOf ?categoryUri.
                ?categoryUri bus:name ?categoryName.
                ?uri ?p ?o.     
                FILTER(?p != bus:image).          
                OPTIONAL
                {
                ?o ?u ?uo.
                FILTER(?u != bus:description).
                }
                FILTER(!REGEX(?uo,'http','i') || !bound(?uo)).
                FILTER((REGEX(?o,'".$this->iId."','i') && !REGEX(?o,'http','i')) || REGEX(?uo,'".$this->iId."','i')).
                }";
                $aDatas = $oDatabase->loadQuery($sQuery);
                $aResults = $aDatas['result']['rows'];
                if(!empty($aResults) && count($aResults) > 0)
                {
                    $this->aParams['aProducts'] = $aResults;
                }
            }
        }
    }
?>
